<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Device extends Model
{
    public $fillable = [
    	'code',
        'status',
    	'recharge_mode',
    ];

    const STATUS_WORK = 0; //使用中
    const STATUS_HALTING = 1;	//停机中
    const STATUS_HALT = 2;	//停机

    protected $appends = [
        'status_description',
    ];

    public static $_STATUS_DESCRIPTION = [
        self::STATUS_WORK => '使用中',
        self::STATUS_HALTING => '停机中',
        self::STATUS_HALT => '停机',
    ];

    public function getStatusDescriptionAttribute()
    {
        if(array_key_exists('status', $this->attributes))
        {
            return self::$_STATUS_DESCRIPTION[$this->attributes['status']];
        }
    }

    public function userInfos()
    {
        return $this->hasMany('App\Models\UserInfo', 'current_device_id', 'id');
    }

    public function installInfo()
    {
        return $this->hasOne('App\Models\InstallInfo', 'device_code', 'code');
    }

    public function afterServices()
    {
        return $this->hasMany('App\Models\AfterService', 'device_id', 'id');
    }

}
